//
//  liwushuoViewController.m
//  UI20_作业礼物说
//
//  Created by 王阳阳 on 16/1/5.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "liwushuoViewController.h"
#import "NetWorkHandler.h"
#import "liwushuoTableViewCell.h"
#import "liwushuo.h"
#import "UIImageView+WebCache.h"
#import "scrollModel.h"
#import "scrollsmoll.h"
static NSInteger m = 0;
@interface liwushuoViewController ()<UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>
@property (nonatomic, strong)UITableView *tableview;
@property (nonatomic, strong)UIScrollView *sv;
@property (nonatomic, strong)NSMutableArray *DarrArr;
@property (nonatomic, strong)NSMutableArray *Sarray;
@property (nonatomic, strong)UIImageView *imageView;
@property (nonatomic, strong)NSTimer *timer;
@property (nonatomic, strong)UIScrollView *sv1;
@property (nonatomic, strong)UIView *view1;
@property (nonatomic, strong)NSMutableArray *Sarray1;
@property (nonatomic, strong)UIImageView *imagesmall;
@property (nonatomic, strong)UIScrollView *sv2;
@property (nonatomic, strong)UILabel *label1;
@property (nonatomic, strong)NSMutableArray *array2;
@end

@implementation liwushuoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    

    self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(roll) userInfo:nil repeats:YES];
    

    
    //导航栏按钮
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"search"] style:UIBarButtonItemStylePlain target:self action:@selector(sousuo:)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"denglu"] style:UIBarButtonItemStylePlain target:self action:@selector(denglu:)];
    self.navigationController.navigationBar.backgroundColor= [UIColor whiteColor];
    
    //导航栏颜色
    self.navigationController.navigationBar.barTintColor= [UIColor redColor];
    //导航栏名称
    self.navigationItem.title = @"礼物说";
    
    //导航栏名称颜色
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    [self data];
    [self scrolldata];
    [self table];
    [self scroll];
    
    
    NSArray *arr = @[@"精选",@"穿搭", @"海淘", @"生日",@"涨姿势", @"创意生活", @"纪念日", @"美食",@"生日", @"数码", @"爱运动", @"科技范", @"家居", @"设计感",@"礼物", @"送基友", @"送爸妈", @"爱动漫", @"送女票"];
    
    self.sv2 = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, 325, 30)];
    self.sv2.delegate = self;
    self.sv2.backgroundColor = [UIColor whiteColor];
    self.sv2.contentSize = CGSizeMake(80 * 18, 0);
    [self.view addSubview:self.sv2];
    
    for (NSInteger i  = 0; i <= 18; i++) {
        
//        self.label1 = [[UILabel alloc]initWithFrame:CGRectMake(90 * i, 0, 80, 30)];
//        self.label1.textAlignment = NSTextAlignmentCenter;
//        self.label1.font = [UIFont fontWithName:@"AppleGothic" size:15];
//        self.label1.textColor = [UIColor grayColor];
//        self.label1.text = arr[i];
//        [self.sv2 addSubview:self.label1];
       
        
        
        UIButton *but = [UIButton buttonWithType:UIButtonTypeSystem];
        but.frame = CGRectMake(90 * i, 0, 80, 30);
        [but setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [but setTitle:arr[i] forState:UIControlStateNormal];
           self.label1.font = [UIFont fontWithName:@"AppleGothic" size:15];
        [self.sv2 addSubview:but];
   
        
    }

    UIButton *but1 = [UIButton buttonWithType:UIButtonTypeCustom];
    but1.frame =  CGRectMake(325, 64, 50, 30);
    but1.backgroundColor = [UIColor whiteColor];
    [but1 setImage:[UIImage imageNamed:@"sanjiao"] forState:UIControlStateNormal];
    [but1 addTarget:self action:@selector(dianji:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:but1];
    
    
    
    
    
}
- (void)dianji:(UIButton *)but1
{
    
    
}
//数据解析
- (void)data
{
    NSString *str = @"http://api.liwushuo.com/v2/channels/101/items?limit=20&ad=2&gender=1&offset=0&generation=2";
    [NetWorkHandler GETWithURL:str completion:^(id result) {
        NSArray *arr = result[@"data"][@"items"];
        _DarrArr = [liwushuo modelHandlerWithArray:arr];
        [self.tableview reloadData];
    }];
}
//轮播图数据解析
- (void)scrolldata
{
    NSString *path = @"http://api.liwushuo.com/v2/banners";
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:path]];
    id resultscroll = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    NSArray *arr1 = resultscroll[@"data"][@"banners"];
    self.Sarray = [scrollModel modelHandlerWithArray:arr1];
//    [_Sarray addObject:_Sarray.firstObject];
    
    
    NSString *path1 = @"http://api.liwushuo.com/v2/secondary_banners?gender=1&generation=2";
    NSData *datascrll1 = [NSData dataWithContentsOfURL:[NSURL URLWithString:path1]];
    id resultscroll1 = [NSJSONSerialization JSONObjectWithData:datascrll1 options:NSJSONReadingMutableContainers error:nil];
    NSArray *arr2 = resultscroll1[@"data"] [@"secondary_banners"];
    _Sarray1 = [scrollsmoll modelHandlerWithArray:arr2];
    NSLog(@"%@",path1);
   
}



//轮播图
- (void)scroll
{
    
    self.view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 375, 320)];
    [self.tableview addSubview:self.view1];
    self.tableview.tableHeaderView = self.view1;
    
    
    
    
    
    //小轮播图
    
    self.sv1 = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 210, 375, 100)];
    
    [self.view1 addSubview:self.sv1];
    self.sv1.contentSize = CGSizeMake(100 * 7, 0);
    self.sv1.delegate = self;
    scrollsmoll *scrsml = [[scrollsmoll alloc]init];
    
    for (NSInteger i = 0; i < _Sarray1.count; i++) {
        scrsml = _Sarray1[i];
        self.imagesmall = [[UIImageView alloc]initWithFrame:CGRectMake(110 * i, 0, 100, 100)];
        [self.imagesmall sd_setImageWithURL:[NSURL URLWithString:scrsml.image_url]];
        [self.sv1 addSubview:self.imagesmall];
        
    }
    
    self.sv1.pagingEnabled = YES;
    self.sv1.bounces = YES;
    
    
  
    
    
    //轮播图1
    self.sv = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 30, 375, 180)];
   [self.view1 addSubview:self.sv];
    self.sv.contentSize = CGSizeMake(375 * _Sarray.count, 0);
    
    self.sv.delegate = self;
    scrollModel *scr = [[scrollModel alloc]init];
    for (NSInteger i = 0; i < _Sarray.count; i++) {
        scr = _Sarray[i];
        self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(375 * i, 0, 375, 160)];
        [self.imageView sd_setImageWithURL:[NSURL URLWithString:scr.image_url]];
     
        [self.sv addSubview:self.imageView];

    }
    

    self.sv.pagingEnabled = YES;
    self.sv.bounces = YES;
    
    
    
    
//    scrollModel *rightModel = _Sarray[0];
//    UIImageView *rightImg = [[UIImageView alloc] init];
//    rightImg.frame = CGRectMake(self.sv.frame.size.width * 6, 0, self.sv.frame.size.width, 180);
//    [rightImg setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:rightModel.image_url]]]];
//    [_Sarray addObject:rightImg];
//    
    
    
    
    UIPageControl *page = [[UIPageControl alloc]initWithFrame:CGRectMake(120, 150, 150, 30)];
//    page.backgroundColor = [UIColor blackColor];
    page.numberOfPages = 7;
    page.tag = 1000;
    page.currentPageIndicatorTintColor = [UIColor grayColor];
    [self.tableview addSubview:page];
    
    [page addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
    
}
//






//tableview
- (void)table
{
 
    self.tableview = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    [self.view addSubview:self.tableview];
    
    [self.tableview registerClass:[liwushuoTableViewCell class] forCellReuseIdentifier:@"cell"];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _DarrArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    liwushuoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    liwushuo *li = _DarrArr[indexPath.row];
    [cell.image1 sd_setImageWithURL:[NSURL URLWithString:li.cover_image_url]];
    cell.label1.text = li.title;
    cell.label2.text = li.likes_count.stringValue;
    
    
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200;
}



- (void)roll
{
    
    
    [self.sv setContentOffset:CGPointMake(self.sv.contentOffset.x + 375, 0) animated:YES];
    //从头滚
    if (self.sv.contentOffset.x == 375 * 5) {
        self.sv.contentOffset = CGPointZero;
    }
    

    UIPageControl *path = (UIPageControl *)[self.view viewWithTag:1000 ];
    UIScrollView *pa = (UIScrollView *)[self.view viewWithTag:2000 ];
    
    //    path.currentPage ++;
    //    m++;
    if (m == 7) {
        m = 0;
        [pa setContentOffset:CGPointMake(7 * pa.frame.size.width, 0) animated:YES ];
        path.currentPage = 0;
        
        
    }
    
    else
    {
        if (m == 0) {
            [pa setContentOffset:CGPointMake(0, 0)];
        }
        m++;
        path.currentPage ++;
        [pa setContentOffset:CGPointMake(path.currentPage * pa.frame.size.width, 0) animated:YES ];
        
        
    }
    
    //
    
    

}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    UIPageControl *page = (UIPageControl *)[self.view viewWithTag:1000];
    page.currentPage = scrollView.contentOffset.x / scrollView.frame.size.width;
}
- (void)pageAction:(UIPageControl *)page
{
    UIScrollView *sv = (UIScrollView *)[self.view viewWithTag:2000];
    [sv setContentOffset:CGPointMake(page.currentPage * sv.frame.size.width, 0) animated:YES];
}









#pragma mark - 拖拽
//开始拖拽时停止timer
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [_timer invalidate];//timer停止
    _timer = nil;//清空timer
}


//停止拖拽时 启动timer
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(roll) userInfo:nil repeats:YES];
    
}


- (void)sousuo:(UITabBarItem *)so
{
    
}
- (void)denglu:(UITabBarItem *)de
{
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
